<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>vue2的深层响应式</h2>
      <p>obj: {{ obj }}</p>
      <p>arr: {{ arr }}</p>
      <button v-on:click="handleClick">btn</button>
    </div>

    <script src="./lib/vue2.js"></script>

    <script>
      new Vue({
        data() {
          return {
            obj: {
              name: "pengshao",
              age: 20,
            },
            arr: [1, 2, 3, 4, 5],
          };
        },
        methods: {
          /*
            vue2中会导致响应式失效的3种情况
              1. 在对象里面挂载新属性的时候
              2. 直接修改数组的长度
              3. 通过索引去修改数组项
          */
          handleClick() {
            // this.obj.sex = 1;
            // this.obj = { ...this.obj };
            // this.obj = Object.assign({}, this.obj);
            // this.obj = JSON.parse(JSON.stringify(this.obj));

            this.arr.length = 3;
            // this.arr = [...this.arr];
            this.arr = [].concat(this.arr);

            // console.log(this.arr);
            // this.arr[2] = 30;
          },
        },
      }).$mount("#app");
    </script>
  </body>
</html>
